# @File    : overtime_order.py
# @Project : web_learn
import time
from datetime import datetime

from selenium import webdriver
from selenium.webdriver.common.by import By

from common.commonUtil import CommonUtil
from common.localUtil import LocalUtil
from common.loginUtil import LoginUtil
from common.yamlUtil import YamlUtil
from selenium.webdriver.common.action_chains import ActionChains


class OverTimeOrder:

    def __init__(self):
        self.conf = YamlUtil("local.yaml").read_yaml()['overtime_order']
        self.eip_url = YamlUtil("local.yaml").read_yaml()

    # 提单方法
    def put_order(self):
        # 数据驱动获取数据
        data = YamlUtil("data.yaml").read_yaml()

        # 通用方法实例化
        com = CommonUtil()
        # 调用打开网页方法
        wd = com.open_web()
        # 实例化定位封装方法
        local = LocalUtil(wd)
        # 特殊操作实例化
        ac = ActionChains(wd)

        # 调用登录通用类登录方法
        LoginUtil(wd).login_eip()

        # 点击加班|值班申请
        local.wait_for_element(By.XPATH, f"{self.conf['title']}").click()
        time.sleep(4)

        # 切换加班|值班申请 frame
        wd.switch_to.frame(f"{self.conf['frame_id']}")

        # 点击新增
        new_order = local.wait_for_element(By.XPATH, f"{self.conf['new_order']}")
        time.sleep(1)
        new_order.click()
        time.sleep(5)
        # wd.switch_to.frame("iframesys_first_page")
        # 切到最外层页面iframe
        wd.switch_to.default_content()

        # //iframe[@sourceiframe='iframe5a59b3d4-c6b2-4ba1-9877-52fb43d792f2']
        order_iframe_0 = local.wait_for_elements(By.TAG_NAME, "iframe")
        # order_iframe = wd.find_element(By.XPATH, "//iframe[@sourceiframe='iframe5a59b3d4-c6b2-4ba1-9877-52fb43d792f2']")
        # print(len(order_iframe))

        # 查看标签的具体信息
        # print(order_iframe[0].get_attribute('outerHTML'))
        # print(order_iframe[1].get_attribute('outerHTML'))
        # print(order_iframe[2].get_attribute('outerHTML'))
        # print(order_iframe[3].get_attribute('outerHTML'))
        # print(order_iframe[4].get_attribute('outerHTML'))
        # print(order_iframe[5].get_attribute('outerHTML'))

        # 切换到加班单所在的iframe
        wd.switch_to.frame(order_iframe_0[1])
        order_iframe_1 = local.wait_for_element(By.XPATH, f"{self.conf['overtime_iframe']}")
        wd.switch_to.frame(order_iframe_1)

        # 选择类别
        category_options = local.wait_for_elements(By.XPATH, f"{self.conf['category_options']}")
        # print(len(category_options))
        # print(category_options[0].get_attribute('outerHTML'))
        # print(category_options[1].get_attribute('outerHTML'))
        # print(category_options[2].get_attribute('outerHTML'))
        time.sleep(1)
        category_options[2].click()
        time.sleep(1)

        overtime_category = local.wait_for_elements(By.XPATH, f"{self.conf['overtime_category']}")
        # 判断加班类型
        if data['overtime_category'] == "正常加班值班":
            overtime_category[0].click()
        elif data['overtime_category'] == "周末或节假日":
            overtime_category[1].click()
        time.sleep(1)
        # 点击确认
        local.wait_for_element(By.XPATH, f"{self.conf['confirm']}").click()

        # 填写加班事由（textarea通过id或者name=Reason定位textarea，然后send_keys）
        time.sleep(1)
        overtime_cause = local.wait_for_element(By.XPATH, f"{self.conf['overtime_cause']}")
        overtime_cause.send_keys(f"{data['overtime_cause']}")

        # 选择加班类型（div id=c9509836-852e-6ddf-edce-71a595d90805）
        overtime_type = wd.find_elements(By.CSS_SELECTOR, f"{self.conf['overtime_type']}")

        # 点击加班类型弹出选择窗口
        ac.move_to_element(overtime_type[0]).click().perform()

        # d = local.wait_for_elements(By.XPATH, "//div[@class='ui-sticky-popup ui-sticky-popupList']")
        # print(d[1].get_attribute('outerHTML'))
        # time.sleep(1)

        # ac.move_to_element(b).click().perform()
        time.sleep(2)
        type_selection = local.wait_for_elements(By.XPATH, f"{self.conf['overtime_type_select']}")
        # print(len(type_selection))
        # print(type_selection[4].get_attribute('outerHTML'))
        time.sleep(1)
        # 判断计算加班费或者调休
        if data['overtime_type'] == "计算加班费/补贴":
            overtime_pay = type_selection[4].find_element(By.XPATH, f"{self.conf['overtime_pay']}")
            overtime_pay.click()
        elif data['overtime_type'] == "调休":
            rest_day = type_selection[4].find_element(By.XPATH, f"{self.conf['rest_day']}")
            rest_day.click()

        # tx = local.wait_for_element(By.XPATH, "//div[label[text()='调休']]")
        # l = d[1].find_element(By.XPATH, ".//div//div[@title='调休']")

        # ac.move_to_element(p).perform()
        # ac.click(p).perform()
        # rs = tx.find_element(By.XPATH, ".//div[@class='item']/input")
        # print(rs.get_attribute('outerHTML'))

        # 加班开始时间选择
        """ 
            1.先定位到input输入框
            2.再获取input输入框的父节点div
            3.根据父节点div获取其兄弟节点span title=日历
            4.点击日历选择日期
            5.选择时间
        """
        # 点击日历图标
        local.wait_for_element(By.XPATH, f"{self.conf['start_time']}").click()
        time.sleep(1)
        # 解析时间字符串
        start_dt = datetime.strptime(data['start_time'], "%Y-%m-%d %H:%M")
        # 调用通用方法中选择年月方法选中正确年月
        com.select_year_month_component(wd, start_dt.year, start_dt.month)
        # (dt.year=年份，dt.month=月份，dt.day=天，dt.hour=小时，dt.minute=分钟)
        # 选择日期
        start_day = local.wait_for_element(By.XPATH,
                                           f"//td[@lay-ymd='{start_dt.year}-{start_dt.month}-{start_dt.day}']")
        start_day.click()
        time.sleep(1)

        # 选择时间
        """
            1.点击选择时间
            2.选择小时
            3.选择分钟
            4.点击返回日期或者点击确定
        """
        # 点击选择时间
        local.wait_for_element(By.XPATH, f"{self.conf['select_time']}").click()
        time.sleep(1)
        # 选择小时
        com.select_time_component(wd, local, "时", str(start_dt.hour))
        # 选择分钟
        com.select_time_component(wd, local, "分", str(start_dt.minute))
        # 点击确定
        local.wait_for_element(By.XPATH, f"{self.conf['confirm_time']}").click()

        # 加班结束时间选择
        """ 
            1.先定位到input输入框
            2.再获取input输入框的父节点div
            3.根据父节点div获取其兄弟节点span title=日历
            4.点击日历选择日期
            5.选择时间
        """
        # 点击日历图标
        local.wait_for_element(By.XPATH, f"{self.conf['end_time']}").click()
        time.sleep(1)
        # 解析时间字符串
        end_dt = datetime.strptime(data['end_time'], "%Y-%m-%d %H:%M")
        # 调用通用方法中选择年月方法选中正确年月
        com.select_year_month_component(wd, end_dt.year, end_dt.month)
        # (dt.year=年份，dt.month=月份，dt.day=天，dt.hour=小时，dt.minute=分钟)
        # 选择日期
        end_day = local.wait_for_element(By.XPATH,
                                         f"//td[@lay-ymd='{end_dt.year}-{end_dt.month}-{end_dt.day}']")
        end_day.click()
        time.sleep(1)

        # 选择时间
        """
            1.点击选择时间
            2.选择小时
            3.选择分钟
            4.点击返回日期或者点击确定
        """
        # 点击选择时间
        local.wait_for_element(By.XPATH, f"{self.conf['select_time']}").click()
        time.sleep(1)
        # 选择小时
        com.select_time_component(wd, local, "时", str(end_dt.hour))
        # 选择分钟
        com.select_time_component(wd, local, "分", str(end_dt.minute))
        # 点击确定
        local.wait_for_element(By.XPATH, f"{self.conf['confirm_time']}").click()

        time.sleep(2)
        # input title/value=保存并提交
        local.wait_for_element(By.XPATH, f"{self.conf['save_order']}").click()

        # 关闭浏览器
        # wd.quit()



